<template>
  <div id="asn"
       v-loading="loading">
    <v-tableTitle :btnConfig="btnConfig"
                  :title="$t('in.deliverAssCode')"
                  @save="save"></v-tableTitle>

    <div id="Asndetails">

      <v-details :inputConfig="inputConfig"
                 :detailData='inputData'
                 :rules="rules"
                 ref="input"
                 @pop="setdata"></v-details>
    </div>

  </div>
</template>

<script type="text/ecmascript-6">
import tableTitle from "commons/TableTitle";
import baseMixin from "@/mixins/BaseMixin";
import config from "./Config";
import bus from "../../../../bus/bus"
import details from '@/components/commons/Detail'


export default {
  name: "barcode",
  mixins: [baseMixin, config],
  data () {

    //自定义验证规则,验证LPN是否合法
    const lpnValidate = (rule, value, callback) => {
      if (value != null && value != '') {
        this.$api.get(`/checkLpn/` + value).then(res => {
          
            callback()
        });
      } else {
        callback(new Error())
      }
    }

    //自定义验证规则,验证LPN是否合法
    const cfqtyValidate = (rule, value, callback) => {
      let reg = /^\+?[1-9]\d*$/;//大于零的正整数
      if (!reg.test(value)) {
        this.$message.error(this.$t('app.inputGreaterZeroNumberPlease'));
        this.inputData.cfqty = ''
        callback(new Error())
      } else {
        if (value > this.inputData.qty) {
          this.$message.error(this.$t('msg.msg38'));
          this.inputData.cfqty = ''
          callback(new Error())
        } else {
          callback()
        }

      }
    }

    return {
      loading: false,
      inputData: {
        receiptkey: "",
        lineno: "",
        lpn: "",
        cfqty: ""
      },
      tableSelectData: [],
      tableSearhData: {},
      rules: {
        receiptkey: [
          { required: true, message: "请填写ASN/收货单", trigger: 'blur' },
        ],
        lineno: [
          { required: true, message: "请填写行号", trigger: 'blur' },
        ],
        lpn: [
          { required: true, message: '请填写正确的lpn', trigger: 'blur', validator: lpnValidate },
        ],
        cfqty: [
          { required: true, message: '请填写正确的数量', trigger: 'blur', validator: cfqtyValidate },
        ],
      },
      choseFormData: {}
    };
  },
  computed: {
    inputConfig () {
      return [
        [
          {
            type: "table",
            label: this.$t('in.asn'),
            disabled: false,
            props: "receiptkey",
            tableConfig: [
              {
                label: this.$t('in.receiptkey'),
                prop: "receiptkey",
                width: '100px',
                input: "input",
                disabled: false
              },
              { label: this.$t('config.shipper'), prop: "storerkey", input: "input", disabled: false },
              {
                label: this.$t('in.externreceiptkey1'),
                prop: "externreceiptkey",
                input: "input",
                disabled: false
              },
              { label: this.$t('in.pokey1'), prop: "pokey", input: "input", disabled: false },
              {
                label: this.$t('in.externpokey'),
                prop: "referencedocument",
                input: "input",
                disabled: false
              },
              {
                label: this.$t('common.status'),
                prop: "status",
                input: "select",
                disabled: false,
                options: this.getOptions("codelkup/RECSTATUS")
              },
              {
                label: this.$t('common.type'),
                prop: "type",
                input: "select",
                width: '100px',
                disabled: false,
                options: this.getOptions("codelkup/RECEIPTYPE")
              },
              { label: this.$t('in.whether'), prop: "whether" },
              {
                label: this.$t('config.carrierCode'),
                prop: "carriername",
                input: "input",
                disabled: false
              },
              {
                label: this.$t('in.carrierreference1'),
                prop: "carrierreference",
                input: "input",
                disabled: false
              },
              {
                label: this.$t('config.supplier'),
                prop: "suppliername",
                input: "input",
                disabled: false
              },
              {
                label: this.$t('in.rtxdelorderno'),
                prop: "rtxdelorderno",
                input: "input",
                disabled: false
              },
              {
                label: this.$t('in.rtxpofromhourse1'),
                prop: "rtxpofromhourse",
                input: "input",
                disabled: false
              },
              {
                label: this.$t('in.fdescription'),
                prop: "fdescription",
                input: "input",
                disabled: false
              },
              {
                label: this.$t('in.qtyexpected'),
                prop: "totalqtyexpected",
                input: "input",
                disabled: true
              },
              {
                label: this.$t('in.qtyreceived'),
                prop: "totalqtyreceived",
                input: "input",
                disabled: true
              },
              {
                label: this.$t('in.lastreceiptdate'),
                prop: "receiptdate",
                input: "date",
                disabled: false
              },
              {
                label: this.$t('in.closeddate1'),
                prop: "closeddate",
                input: "date",
                disabled: false
              }
            ],
            url: `queryReceiptList/`,
          },
         /* {
            type: "input",
            label: this.$t('in.lottable09'),
            disabled: true,
            props: "lottable06"
          },*/
          {
            type: "input",
            label: this.$t('in.lpn'),
            disabled: false,
            props: "lpn",
          },

        ],
        [
          {
            type: "table",
            label: this.$t('outbound.userlinenumber'),
            disabled: false,
            props: "lineno",
            tableConfig: [

              {
                prop: "receiptlinenumber",
                label: this.$t('outbound.userlinenumber'),
                input: "input",
                disabled: false
              },
              {
                prop: "storerkey",
                label: this.$t('config.shipper'),
                input: "input",
                disabled: false
              },
              {
                prop: "sku",
                label: this.$t('config.sku'),
                input: "input",
                disabled: false
              },
              {
                prop: "descr",
                label: this.$t('common.desc'),
                input: "input",
                disabled: true
              },
              {
                prop: "packkey",
                label: this.$t('config.packaging'),
                input: "input",
                disabled: false
              },
              {
                prop: "uom",
                label: this.$t('config.measureUnit'),
                input: "input",
                disabled: false
              },
              {
                prop: "conditioncode",
                label: this.$t('config.freezeCode'),
                input: "input",
                disabled: false
              },
              {
                prop: "toid",
                label: this.$t('in.lpn'),
                input: "input",
                disabled: false
              },
              {
                prop: "toloc",
                label: this.$t('config.loc'),
                input: "input",
                disabled: false
              },
              {
                prop: "pokey",
                label: "PO/SO",
                input: "input",
                disabled: false
              },
              {
                prop: "status",
                label: this.$t('common.status'),
                disabled: false,
                input: "select",
                options: this.getOptions("codelkup/RECSTATUS")
              },
              {
                prop: "qtyexpected",
                label: this.$t('in.totalqtyexpected'),
                input: "input",
                disabled: false
              },
              {
                prop: "qtyreceived",
                label: this.$t('in.qtyreceived'),
                input: "input",
                disabled: false
              }
            ],
            url: 'queryReceiptDetailList/' + this.choseFormData.receiptkey + '/',
          },
         /* {
            type: "input",
            label: this.$t('in.lottable06'),
            disabled: true,
            props: "lottable08",
          },*/
          {
            type: "input",
            label: this.$t('config.num'),
            disabled: false,
            props: "cfqty"
          },
        ],
        [
          {
            type: "input",
            label: this.$t('config.sku'),
            disabled: true,
            props: "sku"
          },
          {
            type: "input",
            label: this.$t('outbound.uom'),
            disabled: true,
            props: "uom"
          },
        ],
        [{
          type: "input",
          label: this.$t('security.name'),
          disabled: true,
          props: "descr"
        },
        {
          type: "input",
          label: this.$t('in.noBindQty'),
          disabled: true,
          props: "qty"
        },]
      ]
    },
    btnConfig () {
      return [
        { label: this.$t('common.save'),  certigier: !this.btnPermNameList.includes('WM010801'), invalid: "save" },
      ]
    }
  },
  created () {
  },
  methods: {

    setdata (data, id, close, listId) {
      this.choseFormData = data;//选中表格的一行,赋值给对象
      if (id == 0 && listId == 0) {
        this.inputData.receiptkey = data.receiptkey;
      } else if (id == 0 && listId == 1) {
        this.inputData.lineno = data.receiptlinenumber;
        this.inputData.sku = data.sku;
        this.inputData.descr = data.descr;
        this.inputData.uom = data.uom;
        this.inputData.qty = data.qtyexpected;
      }
      close(false);
    },
     save () {
      /*let flag = this.$refs.input.submitForm("from");
      console.log(flag);
      console.log(this.$refs.input.$refs["from"]);
      if (!flag) {
        return;
      }*/
       this.$refs.input.$refs["from"].validate((valid) => {
                 if (valid) {
                   this.$api.post(`/saveRtxBarCode`, this.inputData).then(res => {
                     
                       this.$message.success(this.$t('app.saveMes'));
                   }).catch(e => {
                     this.$message.error(e);
                   });
                 }
       });


    }
  },
  mounted () {

  },
  watch: {},
  components: {
    "v-tableTitle": tableTitle,
    'v-details': details
  }
};
</script>

<style scoped>
#asn {
  width: 100%;
  height: 99%;
  padding-top: 6px;
}
</style>
